#pragma once
#include <time.h>
#include <iostream>
#include <stdio.h>
#include <string>
#include <stdarg.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <cstdlib>

using std::cout;
using std::endl;

const int MAX_LOG_SIZE = 1024;
// 打开的文件名
const char* LOG_FILE_NAME = "log.txt";

// 日志等级
enum LEVEL
{
    INFO = 1,
    WARNING,
    ERROR,
    FATAL,
    DEBUG
};

// 打印方式
enum PRINT_METHOD
{
    SCREEN = 1, 
    ONE_FILE,
    MULTIPLE_FILE
};

class Log
{
public:
    Log(PRINT_METHOD printMethod = SCREEN) 
    : _printMethod(printMethod), _path("./log/"){}

    std::string LevelToString(LEVEL level)
    {
        switch (level)
        {
        case INFO:
            return "INFO";
            break;
        case WARNING:
            return "WARNING";
            break;
        case ERROR:
            return "ERROR";
            break;
        case FATAL:
            return "FATAL";
            break;
        case DEBUG:
            return "DEBUG";
            break;
        default:
            return "NONE";
            break;
        }
    }

    // 更改打印方式
    void ChangePrintMethod(PRINT_METHOD printMethod)
    {
        _printMethod = printMethod;
    }

    /*需要将字符串处理为: 默认部分+自定义部分*/
    void LoadMessage(LEVEL level, const char *format, ...)
    {
        // 默认部分, 时间
        char leftBuffer[MAX_LOG_SIZE] = {0};
        time_t t = time(nullptr);
        struct tm *ctime = localtime(&(t));
        snprintf(leftBuffer, sizeof(leftBuffer), "[%s][%d-%d-%d %d:%d:%d]",
                 LevelToString(level).c_str(), ctime->tm_year + 1900,
                 ctime->tm_mon + 1, ctime->tm_mday, ctime->tm_hour,
                 ctime->tm_min, ctime->tm_sec, LevelToString(level).c_str());

        // 自定义部分, 用户输入
        char rightBuffer[MAX_LOG_SIZE] = {0};
        // 因为定义了可变参数, 所以需要处理一下
        va_list args;
        va_start(args, format);
        vsnprintf(rightBuffer, sizeof(rightBuffer), format, args);
        va_end(args);

        // 将两者加到一起
        char logTxt[MAX_LOG_SIZE * 2] = {0};
        snprintf(logTxt, sizeof(logTxt), "%s %s\n", leftBuffer, rightBuffer);

        // cout << logTxt;
        PrintLog(level, logTxt);
    }

    // 通过不同的输出方式, 将logTxt打印到不同的地方
    void PrintLog(LEVEL level, const std::string& logTxt)
    {
        switch (_printMethod)
        {
        case SCREEN:
            cout << logTxt; 
            break;
        case ONE_FILE:
            PrintOneFile(LOG_FILE_NAME, logTxt);
            break;
        case MULTIPLE_FILE:
            PrintMultipleFile(level, logTxt);
            break;
        default:
            break;
        }
    }

    // 向一个文件中写
    void PrintOneFile(const std::string& fileName, const std::string& logTxt)
    {
        std::string logName = _path + fileName;     // 将日志文件写到log文件夹下
        int fd = open(logName.c_str(), O_CREAT | O_APPEND | O_WRONLY, 0666);
        if(fd == -1)    return;
        write(fd, logTxt.c_str(), logTxt.size());
        close(fd);
    }

    // 向多个文件中写
    void PrintMultipleFile(LEVEL level, const std::string& logTxt)
    {
        // 新的文件名: level+LOG_FILE_NAME
        std::string fileName = LevelToString(level) + LOG_FILE_NAME;
        PrintOneFile(fileName, logTxt);
    }

    void operator()(LEVEL level, const char *format, ...)
    {
        // 重载(), 调用更方便一点
        // 默认部分, 时间
        char leftBuffer[MAX_LOG_SIZE] = {0};
        time_t t = time(nullptr);
        struct tm *ctime = localtime(&(t));
        snprintf(leftBuffer, sizeof(leftBuffer), "[%s][%d-%d-%d %d:%d:%d]",
                 LevelToString(level).c_str(), ctime->tm_year + 1900,
                 ctime->tm_mon + 1, ctime->tm_mday, ctime->tm_hour,
                 ctime->tm_min, ctime->tm_sec, LevelToString(level).c_str());

        // 自定义部分, 用户输入
        char rightBuffer[MAX_LOG_SIZE] = {0};
        // 因为定义了可变参数, 所以需要处理一下
        va_list args;
        va_start(args, format);
        vsnprintf(rightBuffer, sizeof(rightBuffer), format, args);
        va_end(args);

        // 将两者加到一起
        char logTxt[MAX_LOG_SIZE * 2] = {0};
        snprintf(logTxt, sizeof(logTxt), "%s %s", leftBuffer, rightBuffer);

        // cout << logTxt;
        PrintLog(level, logTxt);
    }
private:
    PRINT_METHOD _printMethod;
    std::string _path;
};
